/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.builder;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;

/**
 * This class will configure the tools associated with a fan project.
 * @author BEGAUDEAU stephane
 */
public class FanNature implements IProjectNature {
	/**
	 * ID of this project nature.
	 */
	public static final String NATURE_ID = "FanEclipse.fanNature";

	/**
	 * The current project.
	 */
	private IProject project;
	
	/**
	 * The constructor.
	 */
	public FanNature() {
		super();
	}
	
	/**
	 * This method associate a fan project with the fan builder.
	 * @throws CoreException If we can't get the project's description. 
	 * @see org.eclipse.core.resources.IProjectNature#configure()
	 */
	public final void configure() throws CoreException {
		IProjectDescription desc = project.getDescription();
		ICommand[] commands = desc.getBuildSpec();
		for (int i = 0; i < commands.length; ++i) {
			if (commands[i].getBuilderName().equals(FanBuilder.BUILDER_ID)) {
				return;
			}
		}
		
		ICommand[] newCommands = new ICommand[commands.length + 1];
		System.arraycopy(commands, 0, newCommands, 0, commands.length);
		ICommand command = desc.newCommand();
		command.setBuilderName(FanBuilder.BUILDER_ID);
		newCommands[newCommands.length - 1] = command;
		desc.setBuildSpec(newCommands);
		project.setDescription(desc, null);
	}

	/**
	 * This method suppress the link between a fan project and the fan builder.
	 * @throws CoreException If we can't get the project's description.
	 * @see org.eclipse.core.resources.IProjectNature#deconfigure()
	 */
	public final void deconfigure() throws CoreException {
		IProjectDescription description = getProject().getDescription();
		ICommand[] commands = description.getBuildSpec();
		for (int i = 0; i < commands.length; ++i) {
			if (commands[i].getBuilderName().equals(FanBuilder.BUILDER_ID)) {
				ICommand[] newCommands = new ICommand[commands.length - 1];
				System.arraycopy(commands, 0, newCommands, 0, i);
				System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1);
				description.setBuildSpec(newCommands);
				return;
			}
		}
	}

	/**
	 * Getter of the project.
	 * @return The current project.
	 * @see org.eclipse.core.resources.IProjectNature#getProject()
	 */
	public final IProject getProject() {
		return project;
	}

	/**
	 * Setter of the project.
	 * @param project The current project.
	 * @see org.eclipse.core.resources.IProjectNature#setProject(org.eclipse.core.resources.IProject)
	 */
	public final void setProject(final IProject project) {
		this.project = project;
	}

}
